package cn.ggb.eduservice.controller.front;

import cn.ggb.commonutils.JwtUtils;
import cn.ggb.commonutils.OrderVo.EduCourseVo;
import cn.ggb.commonutils.R;
import cn.ggb.eduservice.client.OrdersClient;
import cn.ggb.eduservice.entity.EduCourse;
import cn.ggb.eduservice.entity.chapter.ChapterVo;
import cn.ggb.eduservice.entity.frontvo.CourseQueryVo;
import cn.ggb.eduservice.entity.frontvo.CourseWebVo;
import cn.ggb.eduservice.service.EduChapterService;
import cn.ggb.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private OrdersClient ordersClient;

    //条件查询带分页查询课程
    @ApiOperation(value = "分页课程列表")
    @PostMapping("getFrontCourseList/{current}/{limit}")
    public R getFrontCourseList(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) CourseQueryVo courseQueryVo){

        Page<EduCourse> pageCourse = new Page<>(current,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseQueryVo);
        return R.ok().data("map",map);
    }

    //课程详情查询
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //查询课程详情上部分信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //查询课程详情下部分章节和小节信息
        List<ChapterVo> chapterVoList = eduChapterService.getChapterVideoByCourseId(courseId);
        //远程调用 根据课程id和用户id查询当前课程是否已经支付过了
        boolean buyCourse = ordersClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVoList).data("isBuy",buyCourse);
    }
    //根据课程id查询课程信息
    @PostMapping("/getCourseInfoOrder/{courseId}")
    public EduCourseVo getCourseInfoVo(@PathVariable String courseId){
        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(courseId);
        EduCourseVo eduCourseVo = new EduCourseVo();
        BeanUtils.copyProperties(baseCourseInfo,eduCourseVo);
        return eduCourseVo;
    }

}
